package com.ymttest.business.service;

import java.io.IOException;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.google.gson.JsonObject;
import com.shop2cn.iapi.subscription.parameter.ApplyChargeBean;
import com.shop2cn.iapi.subscription.parameter.ApplyTryoutBean;
import com.shop2cn.iapi.subscription.parameter.ChargeNotifyBean;
import com.shop2cn.iapi.subscription.parameter.GetSubscriptionBean;
import com.shop2cn.iapi.subscription.service.ApplyChargeCall;
import com.shop2cn.iapi.subscription.service.ApplyTryoutCall;
import com.shop2cn.iapi.subscription.service.ChargeNotifyCall;
import com.shop2cn.iapi.subscription.service.GetSubscriptionCall;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.godmch.subscriptioncharge;
import com.ymttest.database.model.godmch.subscriptionfunc;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.utils.TestDataManager;

public class GodmchSubscriptionService {
	//FuncTypeEnum(订购功能)
	public static final int LIVE=0;//直播
	public static final int PSI=1;//进销存
	public static final int WXC=2;//独立小程序
	
	//SubscriptionStatusEnum（订购状态）
	public static final int UnSubscription=0;//未订购	
	public static final int Tryout=1;//试用中
	public static final int TryoutFinish=2;//试用结束
	public static final int Subscription=3;//订购中
	public static final int SubscriptionEnd=4;//订购到期
	
	/**
	 * 商家申请试用
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static void applyTryout(int mchId,int funcType,int funcPackageId) throws ClientProtocolException, IOException{
		ApplyTryoutBean applytryoutBean = new ApplyTryoutBean();
		ApplyTryoutCall applytryoutCall = new ApplyTryoutCall();
		applytryoutBean.setMchId(mchId);
		applytryoutBean.setFuncType(funcType);
		applytryoutBean.setFuncPackageId(funcPackageId);
		applytryoutCall.setData(false,applytryoutBean);
		applytryoutCall.callService();
	}
	
	
	/**
	 * 查询商户订购和套餐信息
	 * @return 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static JsonObject GetSubscription(int mchId,int funcType) throws Exception{
		GetSubscriptionBean getsubscriptionBean = new GetSubscriptionBean();
		GetSubscriptionCall getsubscriptionCall = new GetSubscriptionCall();
		getsubscriptionBean.setMchId(mchId);
		getsubscriptionBean.setFuncType(funcType);
		getsubscriptionCall.setData(false,getsubscriptionBean);
		getsubscriptionCall.callService();
		return getsubscriptionCall.getJsonObject().getAsJsonObject("data").getAsJsonObject("subscriptionFunc");
	}
	
	
	/**
	 * 商家付费订购套餐
	 * @return 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static JsonObject ApplyCharge(int mchId,int funcType,int funcPackageId) throws Exception{
		ApplyChargeBean applychargeBean = new ApplyChargeBean();
		ApplyChargeCall applychargeCall = new ApplyChargeCall();
		applychargeBean.setMchId(mchId);
		applychargeBean.setFuncType(funcType);
		applychargeBean.setFuncPackageId(funcPackageId);
		applychargeBean.setCallbackUrl("http://www.ymatou.com");
		applychargeBean.setClientIp(TestDataManager.getRandomIp());
		applychargeCall.setData(false,applychargeBean);
		applychargeCall.callService();
		return applychargeCall.getJsonObject();
	}
	
	
	/**
	 * 商家付费状态回调通知
	 * @param 商户id
	 * @return 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static JsonObject ChargeNotify(int mchId) throws Exception{
		ChargeNotifyBean chargenotifyBean = new ChargeNotifyBean();
		ChargeNotifyCall chargenotifyCall = new ChargeNotifyCall();
		subscriptioncharge subscriptioncharge = GodmchSubscriptionService.selectSubscriptionChargeNotPay(mchId).get(0);
		chargenotifyBean.setTradingId(subscriptioncharge.getSubscriptionChargeId());
		chargenotifyBean.setPayTime("20191225144331");
		chargenotifyBean.setPaymentId("20191225144331");
		chargenotifyCall.setData(false,chargenotifyBean);
		chargenotifyCall.callService();			
		return chargenotifyCall.getJsonObject();
	}
	
	
	
	
	/**
	 * 用MchId查询订购服务表
	 * @param 商户id
	 * @return 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static List<subscriptionfunc> selectSubscriptionFuncByMchId(int mchId) throws Exception{
		DefGodMchWapper defGodMchWapper = new DefGodMchWapper();
		List<subscriptionfunc> selectSubscriptionFuncByMchId = defGodMchWapper.selectSubscriptionFuncByMchId(mchId);
		return selectSubscriptionFuncByMchId;
	}
	
	
	/**
	 * 用MchId查询订单缴费表
	 * @param 商户id
	 * @return 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static List<subscriptioncharge> selectSubscriptionChargeByMchId(int mchId) throws Exception{
		DefGodMchWapper defGodMchWapper = new DefGodMchWapper();
		List<subscriptioncharge> selectSubscriptionChargeByMchId = defGodMchWapper.selectSubscriptionChargeByMchId(mchId);
		return selectSubscriptionChargeByMchId;
	}
	
	
	/**
	 *用将该用户的订购结束日期更改为前一天
	 * @param 商户id
	 * @return 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static int UpdateSubscriptionTime(int mchId) throws Exception{
		DefGodMchWapper defGodMchWapper = new DefGodMchWapper();
		int updateSubscriptionTime = defGodMchWapper.UpdateSubscriptionTime(mchId);
		return updateSubscriptionTime;
	}
	
	
	/**
	 * 用MchId查询未交费的订单
	 * @return 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static List<subscriptioncharge> selectSubscriptionChargeNotPay(int mchId) throws Exception{
		DefGodMchWapper defGodMchWapper = new DefGodMchWapper();
		List<subscriptioncharge> selectSubscriptionChargeByMchId = defGodMchWapper.selectSubscriptionChargeNotPay(mchId);
		return selectSubscriptionChargeByMchId;
	}
	
	
	
	
	
	
}
